/**
 * Created by ${PRODUCT_NAME}.
 * User: skylerparr
 * Date: 2/4/11
 * Time: 6:53 PM
 * To change this template use File | Settings | File Templates.
 */
package com.thoughtorigin.display.containers {
import flash.display.DisplayObject;
import flash.events.Event;

public class DefaultScrollContainer implements ScrollContainer {

    private var _container: Container;
    private var _maskedContainer: MaskedContainer;
    private var _hScroll: Scroller;
    private var _vScroll: Scroller;

    private var _horizontalScrollPolicy: String = ScrollPolicy.AUTO;
    private var _verticalScrollPolicy: String = ScrollPolicy.AUTO;

    public function DefaultScrollContainer(container: Container, maskedContainer: MaskedContainer, hScroll: Scroller, vScroll: Scroller) {
        _container = container;
        _maskedContainer = maskedContainer;
        _hScroll = hScroll;
        _vScroll = vScroll;

        _hScroll.useHorizontalScroll(true);

        _vScroll.addEventListener(ScrollEvent.SCROLL_UPDATE, onVerticalScrollUpdate);
        _hScroll.addEventListener(ScrollEvent.SCROLL_UPDATE, onHorizontalScrollUpdate);

        _container.addRawChild(DisplayObject(hScroll));
        _container.addRawChild(DisplayObject(vScroll));

        DisplayObject(_hScroll).visible = false;
        DisplayObject(_vScroll).visible = false;

        _container.addEventListener(ContainerEvent.RAW_CHILDREN_CHANGE, onRawChildrenChange, false, 0, true);
        _container.addEventListener(ContainerEvent.RECALCULATE_BOUNDS, onRawChildrenChange, false, 0, true);
        _maskedContainer.addEventListener(LayoutEvent.LAYOUT_REFRESH, onRawChildrenChange, false, 0, true);
    }

    private function onHorizontalScrollUpdate(event:ScrollEvent):void {
        var percent: Number = event.scroller.percent;
        _container.rawChildContainer.x = (_container.rawChildContainer.width - _maskedContainer.width) * percent * -1;
    }

    private function onVerticalScrollUpdate(event:ScrollEvent):void {
        var percent: Number = event.scroller.percent;
        _container.rawChildContainer.y = (_container.rawChildContainer.height - _maskedContainer.height) * percent * -1;
    }

    private function onRawChildrenChange(event: Event):void {
        if(shouldShowVerticalScroller()) {
            showVerticalScroller();
        } else {
            DisplayObject(_vScroll).visible = false;
        }

        if(shouldShowHorizontalScroller()) {
            showHorizontalScroller();
        } else {
            DisplayObject(_hScroll).visible = false;
        }
    }

    private function shouldShowVerticalScroller():Boolean {
        if(_verticalScrollPolicy == ScrollPolicy.ALWAYS) {
            return true;
        }
        if(_verticalScrollPolicy == ScrollPolicy.AUTO && _container.rawChildContainer.height > _maskedContainer.height) {
            return true;
        }
        return false;
    }

    private function shouldShowHorizontalScroller():Boolean {
        if(_horizontalScrollPolicy == ScrollPolicy.ALWAYS) {
            return true;
        }
        if(_horizontalScrollPolicy == ScrollPolicy.AUTO && _container.rawChildContainer.width > _maskedContainer.width) {
            return true;
        }

        return false;
    }

    private function showHorizontalScroller():void {
        //show the scroll bar
        DisplayObject(_hScroll).visible = true;
        _hScroll.scrollLength = _maskedContainer.width;
        DisplayObject(_hScroll).y = _maskedContainer.height;

    }

    private function showVerticalScroller():void {
        //show the scroll bar
        DisplayObject(_vScroll).visible = true;
        _vScroll.scrollLength = _maskedContainer.height;
        DisplayObject(_vScroll).x = _maskedContainer.width - DisplayObject(_vScroll).width;
    }

    public function set horizontalScrollPolicy(value:String):void {
        _horizontalScrollPolicy = value;
        onRawChildrenChange(null);
    }

    public function set verticalScrollPolicy(value:String):void {
        _verticalScrollPolicy = value;
        onRawChildrenChange(null);
    }
}
}
